package com.warehouse_management.purchase.controller;

import com.alibaba.fastjson.JSON;
import com.warehouse_management.config.PageModel;
import com.warehouse_management.purchase.model.Brand;
import com.warehouse_management.purchase.model.ProductCondition;
import com.warehouse_management.purchase.model.Type;
import com.warehouse_management.purchase.model.TypeCondition;
import com.warehouse_management.purchase.service.ProductTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @PACKAGE_NAME: com.warehouse_management.purchase.controller
 * @NAME: ProductTypeController
 * @USER: ZhouYongCheng
 * @DATE: 2022/10/3
 * @TIME: 2:47
 * @Description: 商品类型管理控制层
 */
@Controller
@RequestMapping("/productType")
public class ProductTypeController {

    @Autowired
    private ProductTypeService productTypeService;

    @RequestMapping("/queryAllType")
    @ResponseBody
    public String queryAllType(@RequestParam("pagenum") int pagenum, TypeCondition type, PageModel<TypeCondition> pageModel){
        pageModel.setPageNum(pagenum);
        PageModel<TypeCondition> pageModel1 = productTypeService.PageTypePosition(type, pageModel);
        if (pageModel1.getData() != null){
            if (!pageModel1.getData().isEmpty()){
                System.out.println(pageModel1);
                return JSON.toJSONString(pageModel1);
            }
        }
        return null;
    }

    /**
     * 连小龙
     * 查询单个产品数据(模糊查询)
     * @param pagenum
     * @param
     * @param
     * @param statu
     * @param pageModel
     * @return
     */
    @RequestMapping("/queryOneType")
    @ResponseBody
    public String queryOneType(@RequestParam("pagenum") int pagenum,@RequestParam("bName") String bName,@RequestParam("tName") String tName,@RequestParam("statu") String statu,PageModel<TypeCondition> pageModel){
        TypeCondition tc = new TypeCondition();
        if (bName.equals("请选择")){
            bName = null;
        }
        if (tName.equals("")){
            tName = null;
        }
        tc.setTypeName(tName);
        tc.setBrandName(bName);
        tc.setTypeStatus(statu);
        pageModel.setPageNum(pagenum);
        PageModel<TypeCondition> typeConditionPageModel = productTypeService.queryTypeListByCondition(tc, pageModel);
        System.out.println(typeConditionPageModel);
        if (typeConditionPageModel != null){
            System.out.println(typeConditionPageModel);
            return JSON.toJSONString(typeConditionPageModel);
        }
        return null;
    }

    @RequestMapping("/queryType")
    @ResponseBody
    public String queryType(){
        List<Type> types = productTypeService.queryType();
        if (types != null){
            System.out.println("aa"+types);
            return JSON.toJSONString(types);
        }
        return null;
    }

    @RequestMapping("/queryBrand")
    @ResponseBody
    public String queryBrand(){
        List<Brand> brands = productTypeService.queryBrand();
        if (brands != null){
            System.out.println(brands);
            return JSON.toJSONString(brands);
        }
        return null;
    }


    @RequestMapping("/addType")
    @ResponseBody
    public String addType(@RequestParam("bName") String bName,@RequestParam("tName") String tName,@RequestParam("status") String status,@RequestParam("uname") String uname, PageModel<TypeCondition> pageModel){

        TypeCondition tc = new TypeCondition();
        if (status.equals("可用")){
            status = "1";
        }else if (status.equals("不可用")){
            status = "0";
        }
        tc.setUname(uname);
        tc.setTypeStatus(status);
        tc.setBrandName(bName);
        tc.setTypeName(tName);
        TypeCondition t = new TypeCondition();
        t.setTypeName(tName);
        t.setBrandName(bName);
        PageModel<TypeCondition> typeConditionPageModel = productTypeService.queryTypeListByCondition(t, pageModel);
        int j = 0;
        for (TypeCondition datum : typeConditionPageModel.getData()) {
            if (!datum.getBrandName().equals(bName)){
                j++;
            }
        }
        if (j>0){
            return JSON.toJSONString("falied");
        }
        int i = productTypeService.addType(tc);
        if (i>0){
            return JSON.toJSONString("true");
        }else {
            return JSON.toJSONString("false");
        }
    }

    @RequestMapping("/updateType")
    @ResponseBody
    public String updateType(@RequestParam("bName") String bName,@RequestParam("tName") String tName,@RequestParam("status") String status,@RequestParam("tId") String tId,PageModel<TypeCondition> pageModel){

        TypeCondition tc = new TypeCondition();
        if (status.equals("可用")){
            status = "1";
        }else if (status.equals("不可用")){
            status = "0";
        }
        tc.setTypeId(tId);
        tc.setTypeName(tName);
        tc.setBrandName(bName);
        tc.setTypeStatus(status);
        TypeCondition t = new TypeCondition();
        t.setTypeId(tId);
        PageModel<TypeCondition> typeConditionPageModel = productTypeService.queryTypeListByCondition(t, pageModel);
        int j = 0;
        for (TypeCondition datum : typeConditionPageModel.getData()) {
            if (!datum.getBrandName().equals(bName) || !datum.getTypeName().equals(tName)){
                j++;
            }
        }
        if (j>0){
            return JSON.toJSONString("falied");
        }
        int i = productTypeService.updateType(tc);
        if (i>0){
            return JSON.toJSONString("true");
        }else {
            return JSON.toJSONString("false");
        }
    }

    @RequestMapping("/updateTypeStatus")
    @ResponseBody
    public String updateTypeStatus(@RequestParam("tId") String tId,@RequestParam("status") String status){


        TypeCondition tc = new TypeCondition();
        if (status.equals("可用")){
            status = "0";
        }else if (status.equals("不可用")){
            status = "1";
        }
        tc.setTypeId(tId);
        tc.setTypeStatus(status);
        int i = productTypeService.updateTypeStatus(tc);
        if (i>0){
            return JSON.toJSONString("true");
        }else {
            return JSON.toJSONString("false");
        }
    }
}
